package com.aw.baselib.util

import android.content.Context
import android.util.DisplayMetrics
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout

/**
 * Created by hzw
 */
object AwDisplayUtil {
    /**
     * 根据dip值转化成px值
     *
     * @param context
     * @param dip
     * @return
     */
    fun dipToPix(context: Context, dip: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dip.toFloat(),
            context.resources.displayMetrics
        ).toInt()
    }

    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     *
     * @param pxValue
     * （DisplayMetrics类中属性density）
     * @return
     */
    fun px2dip(context: Context, pxValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt()
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param dipValue
     * （DisplayMetrics类中属性density）
     * @return
     */
    @JvmStatic
    fun dip2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param pxValue
     * （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    fun px2sp(context: Context, pxValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (pxValue / fontScale + 0.5f).toInt()
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue
     * （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    fun sp2px(context: Context, spValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }

    fun getNavigationBarHeight(context: Context): Int {
        val resources = context.resources
        val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
        val height = resources.getDimensionPixelSize(resourceId)
        Log.d("getNavigationBarHeight", "getNavigationBarHeight: ====$height")
        return getHasVirtualKey(context) - getNoHasVirtualKey(context)
    }

    private fun getNoHasVirtualKey(context: Context): Int {
        val windowManager =
            context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        return windowManager.defaultDisplay.height
    }

    private fun getHasVirtualKey(context: Context): Int {
        var dpi = 0
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = windowManager.defaultDisplay
        val dm = DisplayMetrics()
        val c: Class<*>
        try {
            c = Class.forName("android.view.Display")
            val method = c.getMethod("getRealMetrics", DisplayMetrics::class.java)
            method.invoke(display, dm)
            dpi = dm.heightPixels
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return dpi
    }

    /**
     * 获取屏幕宽度px
     *
     * @param context
     * @return
     */
    fun deviceWidthPX(context: Context): Int {
        return context.applicationContext.resources
            .displayMetrics.widthPixels
    }

    /**
     * 获取屏幕高度px
     *
     * @param context
     * @return
     */
    fun deviceHeightPX(context: Context): Int {
        return context.applicationContext.resources
            .displayMetrics.heightPixels
    }

    /**
     * 动态设置margin
     * @param context
     * @param view
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    fun setMarginInLinearLayout(
        context: Context,
        view: View,
        left: Int,
        top: Int,
        right: Int,
        bottom: Int
    ) {
        val lp = view.layoutParams as LinearLayout.LayoutParams
        lp.setMargins(
            dip2px(context, left.toFloat()),
            dip2px(context, top.toFloat()),
            dip2px(context, right.toFloat()),
            dip2px(context, bottom.toFloat())
        )
        view.layoutParams = lp
    }
}